package com.zbl.zblproject.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbl.zblproject.core.entity.AdminUser;
import com.zbl.zblproject.erp.entity.ClientSalesTable;
import com.zbl.zblproject.erp.entity.ErpTableEntity;
import com.zbl.zblproject.member.entity.Member;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/16
 * 门店用户mapper
 */
@Repository
public interface AdminUserMapper extends BaseMapper<AdminUser> {

    @Select("SELECT * FROM zbl_admin_user WHERE user_name LIKE '%#{name}%' ")
    List<AdminUser> selectByName(@Param("name") String name);

    @Select("SELECT * FROM zbl_admin_user WHERE user_name = #{name} ")
    AdminUser selectOneByName(@Param("name") String name);

    @Select("SELECT user_name FROM zbl_admin_user WHERE user_id = #{id}")
    String selectNameById(@Param("id") int id);

    @Select("SELECT user_id FROM zbl_admin_user WHERE user_id = #{id}")
    Integer selectNameByIds(@Param("id") int id);

    @Select("SELECT email, user_id, user_name FROM zbl_admin_user WHERE user_id = #{id}")
    AdminUser selectUserById(@Param("id") int id);

    @Select("SELECT user_number FROM zbl_admin_user WHERE user_id = #{id}")
    String selectUserNumber(@Param("id") int id);


    @Update("UPDATE zbl_admin_user SET user_number = #{number} WHERE user_id = #{id}")
    boolean updateUserNumber(@Param("id") int id, @Param("userNumber") String number);

    @Select("SELECT ratio,user_id,code_name,user_name,ratio,500000*ratio as 'storeTasks' FROM zbl_admin_user WHERE group_id=#{groupId}")
    List<AdminUser> selectAdminUserByGroupId(@Param("groupId") int groupId);

    /**
     * 减去用户金额
     * @param userId 用户ID
     * @param money  金额
     * @return
     */
    @Update("UPDATE zbl_admin_user SET balance = balance - #{total}  WHERE user_id = #{id} ")
    boolean deleteUserMoney(@Param("id") int userId, @Param("total") BigDecimal money);

    @Update("UPDATE zbl_admin_user SET balance = balance + #{total}  WHERE user_name = #{uName} ")
    boolean deleteUserMoneys(@Param("uName") String uName, @Param("total") BigDecimal money);

    @Update("UPDATE zbl_admin_user SET new_balance = new_balance - #{total}  WHERE user_id = #{id} ")
    boolean deleteUserMoney1(@Param("id") int userId, @Param("total") BigDecimal money);

    @Update("UPDATE zbl_admin_user SET balance = balance - #{total}  WHERE user_name = #{name} ")
    boolean deleteUserMoneyByName(@Param("name") String name, @Param("total") BigDecimal money);

    /**
     * 查询用户余额
     * @param userName 用户名称
     * @return 用户余额
     */
    @Select("SELECT balance FROM zbl_admin_user WHERE user_name = #{userName}")
    BigDecimal selectUserBalanceByUserName(@Param("userName") String userName);

    @Select("SELECT balance FROM zbl_admin_user WHERE user_id = #{userId}")
    BigDecimal selectUserBalanceByUserId(@Param("userId") int id);

    @Select("SELECT * FROM zbl_admin_user WHERE user_id = (SELECT user_id FROM zbl_orders WHERE order_sn = #{orderSn})")
    AdminUser selectUserByOrderSn(@Param("orderSn") String orderSn);


    List<AdminUser> selectNotCGDDBySupplierId(@Param("supplierId") int supplierId,
                                              @Param("start") long start, @Param("end") long end);


    List<ErpTableEntity> selectErpTableEntity(@Param("supplierId") int supplierId,
                                              @Param("start") long start, @Param("end") long end);

    List<String> selectOrderSnByUserForStart(@Param("supplierId") int supplierId,
                                             @Param("userId") int userId,
                                             @Param("start") long start, @Param("end") long end);

    Integer selectNotCGDDGoodsNumberBySupplierIdAndUserId(@Param("supplierId") int supplierId, @Param("userId") int userId,
                                                          @Param("start") long start, @Param("end") long end);

    BigDecimal selectNotCGDDGoodsPriceBySupplierIdAndUserId(@Param("supplierId") int supplierId, @Param("userId") int userId,
                                                          @Param("start") long start, @Param("end") long end);

    List<ClientSalesTable> selectSalesTable();

    ClientSalesTable selectOutTableForSalesAndNumber(@Param("table") ClientSalesTable table,
                                                     @Param("start") LocalDateTime start,
                                                     @Param("end") LocalDateTime end);

    ClientSalesTable selectOutTableForCountAndFinancial(@Param("table") ClientSalesTable table,
                                                        @Param("start") LocalDateTime start,
                                                        @Param("end") LocalDateTime end);
    @Select("SELECT * FROM zbl_member z where z.telephone =#{mobile}")
    Member getVipIntegral(@Param("mobile")String mobile);
}
